//
//  YHStatusOriginalView.swift
//  YHWeibo
//
//  Created by yuhao.mac on 16/6/28.
//  Copyright © 2016年 yuhao.mac. All rights reserved.
//

import UIKit
import SnapKit

class YHStatusOriginalView: UIView {
    
    //声明一个全局变量记录底部约束
    var originalViewBottomConstraint:Constraint?
    
    var status:YUStatus?{
        didSet{
            //用户昵称
            nameLabel.text = status?.user?.name
            //用户头像
            headImageView.sd_setImageWithURL(NSURL(string: status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
            
            //微博内容
            contentLabel.text = status?.text
            //认证图片
            vipImageView.image = status?.user?.verifiedImage
            //会员等级
            expiredImageView.image = status?.user?.mbrankImage
            
            sourceLabel.text = getSourceAttr(status?.source)
            
            timeLabel.text = status?.creatAtArr
            
            
            /*
            （考虑到cell的复用问题）
            01 先卸载原创微博的底部约束
            02 如果status?.pic_urls.count > 0 代表有配图
            - 显示配图
            - 要先赋值 然后进行约束
            - 原创微博的底部 == 配图的底部 + 10（更新约束）
            反之 <= 0  均是没有配图
            - 隐藏配图
            - 原创微博的底部 == 微博正文的底部 + 10（更新约束）
            */
            
            //记录约束。先卸载约束
            originalViewBottomConstraint?.uninstall()
            
            if status?.pic_urls?.count > 0 {
                pictureView.hidden = false
                //赋值
                pictureView.pic_urls = status?.pic_urls
                //更新约束
                self.snp_makeConstraints { (make) -> Void in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StautsMargin).constraint
                }
                
            }else{
                pictureView.hidden = true

                self.snp_makeConstraints { (make) -> Void in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(StautsMargin).constraint
                }
            }
        }
    }
    
    
    
    
    //处理来源信息
    func getSourceAttr(source: String?) ->String{
        guard let s = source where s.containsString("\">") else {
            return "来自火星客户端"
        }
        let start = s.rangeOfString("\">")!
        let end = s.rangeOfString("</a")!
        let result = s.substringWithRange(start.endIndex..<end.startIndex)
        return ("来自\(result)")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
    
        backgroundColor = UIColor.whiteColor()
        
        //添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(expiredImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置约束
        // 头像
        headImageView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(StautsMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        // 昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(StautsMargin)
        }
        // 用户等级
        expiredImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(StautsMargin)
            make.centerY.equalTo(nameLabel)
        }
        // 发布时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        // 来自
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(StautsMargin)
        }
        // 认证
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        //微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(StautsMargin)
        }
        //原创微博配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(StautsMargin)
//            make.size.equalTo(CGSizeMake(100, 100))
        }
        //原创微博
        self.snp_makeConstraints { (make) -> Void in
            originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StautsMargin).constraint
        }
        //临时数据
        nameLabel.text = "昵称"
        timeLabel.text = "时间"
//        print(status?.source)
        
    }
    
    //懒加载控件
    //头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default_big")
    //用户昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15)
    //用户等级
    private lazy var expiredImageView: UIImageView = UIImageView(imgName: "common_icon_membership_expired")
    //发布微博的时间
    private lazy var timeLabel: UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    //来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.grayColor(), textFont: 10)
    //VIP
    private lazy var vipImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    //原创微博内容
    private lazy var contentLabel:UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15, maxWidth: SCREENW - 2*StautsMargin)
    //配图
    private lazy var pictureView:YHPictureView = YHPictureView()

}
